package com.cn.springcloudwechat.service.personalcenter.serviceImpl;


import com.alibaba.fastjson.JSON;
import com.cn.springcloudwechat.common.utils.ErrorUtil;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.mapper.mapper.YmUserChildMapper;
import com.cn.springcloudwechat.mapper.mapper.vaccinestation.YmStationVMapper;
import com.cn.springcloudwechat.model.YmUserChild;
import com.cn.springcloudwechat.model.dto.YmChildDto;
import com.cn.springcloudwechat.model.vaccinestion.YmVStation;
import com.cn.springcloudwechat.redis.RedisUtil;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserChildService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

@Service("YmUserChildService")
public class YmUserChildServiceImpl implements YmUserChildService{


    @Autowired
    YmUserChildMapper ymUserChildMapper;

    @Autowired
    YmStationVMapper ymStationMapper;

    @Override
    public List<YmUserChild> getUserChild(Long userId) {
    	YmVStation YmVStation = new YmVStation();
        List<YmUserChild> ymUserChildren = ThreadLocalUtil.getUserChild(userId);
        if(ymUserChildren != null){
            for(YmUserChild ymUserChild : ymUserChildren){
                if(ymUserChild.getVaccineStationId() != null){
                    YmVStation.setStationId(ymUserChild.getVaccineStationId());
                    YmVStation station = ymStationMapper.selectByPrimaryKeys(YmVStation);
                    if(station != null){
                        ymUserChild.setVaccineStationName(station.getStationName());
                    }
                }
            }
        }else{
           ymUserChildren = ymUserChildMapper.getUserChild(userId);
        }
        return ymUserChildren;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertUserChild(YmUserChild ymUserChild) throws IllegalAccessException {
        Long userId = ThreadLocalUtil.getUser().getUserId();
        ymUserChild.setUserId(userId);
        YmUserChild ymUserChild1 = ymUserChildMapper.getYmUserChildByName(ymUserChild.getChildRealname(),userId);
        if(ymUserChild1 != null){
            throw new IllegalAccessException("已存在该宝宝，请勿重复添加");
        }
        ymUserChild.setChildHeadimgurl("http://cos-vc.cdaxkj.cn/upload/img/1534736461560mmexport1534736641856.jpg");
        Integer result = ymUserChildMapper.insertSelective(ymUserChild);
        //将用户孩子存入redis
        List<YmUserChild> ymUserChildren = ymUserChildMapper.getUserChild(userId);
        ThreadLocalUtil.savaUserChild(ymUserChildren);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateUserChild(YmUserChild ymUserChild) {
        Integer result =  ymUserChildMapper.updateByPrimaryKeySelective(ymUserChild);
        //将用户孩子存入redis
        List<YmUserChild> ymUserChildren = ymUserChildMapper.getUserChild(ymUserChild.getUserId());
        ThreadLocalUtil.savaUserChild(ymUserChildren);
        return result;
    }

    @Override
    public List<YmChildDto> getChildName() {
        Long userId = ThreadLocalUtil.getUser().getUserId();
        return ymUserChildMapper.getChildName(userId);
    }

    @Override
    public YmUserChild selectByPrimaryKey(Long childId) {
        return ymUserChildMapper.selectByPrimaryKey(childId);
    }


}
